package com.mohan.system.manage.controller;

import com.mohan.calligraphy.anno.CallParamValid;
import com.mohan.calligraphy.dto.SystemUserRegistry;
import com.mohan.calligraphy.result.R;
import com.mohan.system.manage.entity.Menu;
import com.mohan.system.manage.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author mohan
 * @since 2022-11-24
 */
@RestController
@RequestMapping("/manage/user")
public class UserController {

    @Resource
    private IUserService userService;

    /**
     * 获取用户登录的角色
     * @return
     */
    @RequiresRoles("admin")
    @GetMapping("/roles")
    public R getUserRoles() {
        System.out.println("登录认证验证角色");
        return R.ok();
    }

    /**
     * 用户登录
     * @param userRegistry
     * @return
     */
    @PostMapping("/login")
    public R login(@CallParamValid  @RequestBody SystemUserRegistry userRegistry) {
        String token = userService.login(userRegistry);
        return R.ok().put("data",token);
    }

    /**
     * 用户注册
     * @param userRegistry
     * @return
     */
    @PostMapping("/register")
    public R register(@CallParamValid @RequestBody SystemUserRegistry userRegistry) {
        userService.register(userRegistry);
        return R.ok();
    }

    /**
     * 获取用户下面的菜单
     * @return
     */
    @GetMapping("/getUserMenu")
    public R getUserMenu() {
        List<Menu> menus = userService.getUserMenu(Boolean.FALSE);
        return R.ok().put("data",menus);
    }

    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("user:delete")
    public R deleteUser(@PathVariable(value = "id") Long id) {
        userService.delete(id);
        return R.ok();
    }
}
